package com.fmblzf.library.adapter;

import android.util.SparseArray;
import android.view.View;

import com.example.administrator.library.R;


/**
 * @设计： ZF
 * @类描述：适配器帮助类
 * @创建时间：2014-09-11
 */
public class ViewHolder {

    private SparseArray<View> sparseArray;//控件缓存
    private static ViewHolder instance;//实例
    /**
     * 构造器
     */
    private ViewHolder(){
    }

    /**
     * 获取对象实例
     * @return
     */
    public static ViewHolder getInstance(){
        if(instance == null) {//暂时这个view不能为空
            instance = new ViewHolder();
        }
        return instance;
    }

    /**
     * 获取具体的控件
     * @param view 不能为空
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T getView(View view ,int viewId){
        sparseArray = (SparseArray<View>) view.getTag(R.string.app_name);
        if(sparseArray == null){
            sparseArray = new SparseArray<View>();
            view.setTag(R.string.app_name,sparseArray);
        }
        View childView = sparseArray.get(viewId);
        if(childView == null){
            childView = view.findViewById(viewId);
            sparseArray.put(viewId,childView);
        }
        return (T)childView;
    }
}
